package ferp.android.widgets.buttons;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import ferp.android.GUI;
import ferp.android.R;

/**
 * User: igorgok
 * Date: 12/30/13 12:25 PM
 */
public abstract class TwoLayerButton extends FrameLayout
{
  public TwoLayerButton(Context context) {this(context, null, 0);}
  public TwoLayerButton(Context context, AttributeSet attrs) {this(context, attrs, 0);}
  public TwoLayerButton(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs, defStyle);

    setForegroundGravity(Gravity.CENTER);

    // button at the lower layer
    button = onCreateButton(context);
    button.setBackgroundResource(getButtonBackgroundResource());

    // create the upper layer
    upper = onCreateUpperLayer(context, attrs);

    addView(button, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    addView(upper, new LayoutParams(getUpperLayerLayoutWidth(), getUpperLayerLayoutHeight(), getUpperLayerLayoutGravity()));
  }

  @Override
  public void setEnabled(boolean enabled)
  {
    super.setEnabled(enabled);

    button.setEnabled(enabled);
    upper.setEnabled(enabled);

    if (mark != null)
      mark.setVisibility(enabled ? VISIBLE : GONE);
  }

  @Override
  public void setVisibility(int visibility)
  {
    super.setVisibility(visibility);

    if (mark != null && visibility != VISIBLE)
    {
      removeView(mark);
      mark = null;
    }
  }

  @Override
  public void setOnClickListener(OnClickListener ocl) {button.setOnClickListener(ocl);}

  public void mark()
  {
    if (mark == null)
    {
      mark = new ImageView(getContext());
      mark.setBackgroundResource(R.drawable.mark);

      addView(mark);
      resizeMark();
    }

    if (isEnabled())
      mark.setVisibility(VISIBLE);
  }

  public void unmark()
  {
    if (mark != null)
      mark.setVisibility(INVISIBLE);
  }

  protected abstract View onCreateUpperLayer(Context context, AttributeSet attrs);
  protected abstract int getButtonBackgroundResource();

  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom)
  {
    if (mark != null)
      resizeMark();

    super.onLayout(changed, left, top, right, bottom);
  }

  protected Button onCreateButton(Context context) {return new Button(context);}

  protected int getUpperLayerLayoutWidth() {return ViewGroup.LayoutParams.MATCH_PARENT;}
  protected int getUpperLayerLayoutHeight() {return ViewGroup.LayoutParams.MATCH_PARENT;}
  protected int getUpperLayerLayoutGravity() {return Gravity.CENTER;}

  protected int getMarkGravity() {return Gravity.CENTER_VERTICAL | Gravity.RIGHT;}

  // button at lower layer
  protected Button button;

  private void resizeMark()
  {
    int size = GUI.Mark.size();
    int margin = GUI.Mark.margin();

    if (lp == null)
      lp = new LayoutParams(size, size, getMarkGravity());
    else
    {
      lp.width = size;
      lp.height = size;
    }

    lp.setMargins(0, 0, margin, margin);
    mark.setLayoutParams(lp);
  }

  // upper view
  private View upper;
  // mark image view
  private ImageView mark;
  // cached FrameLayout.LayoutParams instance
  private FrameLayout.LayoutParams lp;
}
